<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:form="http://java.sun.com/jsf/composite/components/form"
	template="/templates/include/securedmain.xhtml">
	<ui:define name="content">

		<h:form id="form">
			<!-- 		Small workaround as schedule does not initialize backing bean properly -->
			<h:inputHidden value="#{objectCalendarBean.id}"></h:inputHidden>
			<h:outputText id="errorToast" styleClass="errorToast"
				value="#{objectCalendarBean.errorMessage}"></h:outputText>

			<p:schedule value="#{objectCalendarBean.eventModel}"
				widgetVar="myschedule" startWeekday="1" editable="true" locale="cs">


				<p:ajax event="eventMove" update="errorToast"
					listener="#{objectCalendarBean.onEventMove}"
					oncomplete="myschedule.update()" />

				<p:ajax event="eventResize" update="errorToast"
					listener="#{objectCalendarBean.onEventResize}"
					oncomplete="myschedule.update()" />


				<p:ajax event="eventSelect"
					listener="#{objectCalendarBean.onEventSelect}" update=":editDia"
					oncomplete="editDialog.show();" />


				<p:ajax event="dateSelect"
					listener="#{objectCalendarBean.onDateSelect}" update=":eventDia"
					oncomplete="eventDialog.show();" />
			</p:schedule>
		</h:form>


		<p:dialog widgetVar="editDialog"
			header="#{msg['view.actions.calendar.edit']}" id="editDia"
			appendToBody="true" showEffect="clip" hideEffect="clip" modal="true"
			dynamic="true">

			<p:ajax event="close" listener="#{objectCalendarBean.resetEditPanel}" />

			<h:form id="actionform">
				<p:commandButton id="deleteButton"
					actionListener="#{objectCalendarBean.deleteEvent}"
					disabled="#{!objectCalendarBean.event.deletable}"
					value="#{msg['view.actions.delete']}" update="@this"
					icon="ui-icon-trash"
					oncomplete="myschedule.update(); editDialog.hide();" />

				<p:commandButton id="pickUpButton" icon="ui-icon-circle-arrow-n"
					disabled="#{!objectCalendarBean.event.pickable}"
					actionListener="#{objectCalendarBean.pickUpEvent}"
					value="#{msg['view.actions.button.pickup']}" update="@this"
					oncomplete="myschedule.update(); editDialog.hide();" />

				<p:commandButton id="returnItBackButton"
					disabled="#{!objectCalendarBean.event.returnable}"
					icon="ui-icon-circle-arrow-s"
					actionListener="#{objectCalendarBean.returnBackEvent}"
					value="#{msg['view.actions.button.returnitback']}" update="@this"
					oncomplete="myschedule.update(); editDialog.hide();" />
			</h:form>

			<h:form id="editForm">
				<h:inputHidden value="true">
					<f:validator validatorId="reservationValidator" />
					<f:attribute name="since" value="#{sinceEdit}" />
					<f:attribute name="to" value="#{toEdit}" />
				</h:inputHidden>
				
				<h:panelGrid id="eventEdit" columns="2">
				
					<h:outputLabel for="usrList" value="#{msg['properties.user']}:" />
					<p:selectOneMenu filter="true" filterMatchMode="startsWith"
						value="#{objectCalendarBean.event.user}" effect="fade"
						var="selusr" converter="userConverter" id="usrlist">
						<f:selectItems value="#{objectCalendarBean.users}" var="usr"
							itemLabel="#{usr.nickname}" itemValue="#{usr}" id="usrList" />
						<p:ajax event="change" update="@this" />
						<p:column>
							<p:button id="viewButton"
								onclick="window.open('view/viewuser.jsf?id=#{selusr.id}'); return false;"
								title="#{msg['view.actions.view']}" icon="ui-icon-search"
								update="@form" ajax="false">
							</p:button>
						</p:column>
						<p:column>
							#{selusr.nickname}
							</p:column>
					</p:selectOneMenu>



					<h:outputLabel for="perioDisable"
						value="#{msg['properties.period.enabled']}" />
					<p:selectBooleanCheckbox id="perioDisable"
						disabled="#{!objectCalendarBean.event.editable}"
						value="#{objectCalendarBean.event.enabled}" />

					<h:outputLabel for="objtext" value="#{msg['properties.object']}:" />
					<h:link id="objtext" outcome="/secured/view/viewobject"
						value="#{objectCalendarBean.event.object.name}">
						<f:param name="id" value="#{objectCalendarBean.event.object.id}" />
					</h:link>

					<h:outputLabel for="from"
						value="#{msg['properties.period.since']}:" />
					<p:calendar mindate="#{objectCalendarBean.currentDate}" id="from"
						value="#{objectCalendarBean.event.startDate}" binding="#{sinceEdit}" 
						pattern="#{objectCalendarBean.dateViewPattern}" disabled="#{!objectCalendarBean.event.editable}" />

					<h:outputLabel for="toEdit" value="#{msg['properties.period.to']}:" />
					<p:calendar mindate="#{objectCalendarBean.minToDate}" id="toEdit"
						value="#{objectCalendarBean.event.endDate}" binding="#{toEdit}"
						pattern="#{objectCalendarBean.dateViewPattern}" disabled="#{!objectCalendarBean.event.editable}" />

					<h:outputLabel for="pickupState"
						value="#{msg['properties.period.state.pickup']}:" />
					<h:outputText id="pickupState"
						value="#{objectCalendarBean.event.pickStatus}" />
					<h:outputLabel for="returnItBackState"
						value="#{msg['properties.period.state.returnitback']}:" />
					<h:outputText id="returnItBackState"
						value="#{objectCalendarBean.event.returnStatus}" />

					<h:outputLabel for="perioEdit"
						value="#{msg['properties.reservation.periodical']}:" />
					<p:selectBooleanCheckbox id="perioEdit"
						value="#{objectCalendarBean.event.periodical}" disabled="true" />

					<h:outputLabel for="weeks"
						rendered="#{objectCalendarBean.event.periodical}"
						value="#{msg['view.actions.calendar.howmanyweeks']}:" />
					<h:outputText id="weeksEdit"
						value="#{objectCalendarBean.event.numberOfWeeks}"
						rendered="#{objectCalendarBean.event.periodical}" />

				</h:panelGrid>
				<p:commandButton id="editButton"
					disabled="#{!objectCalendarBean.event.editable}"
					actionListener="#{objectCalendarBean.editEvent}"
					icon="ui-icon-check" value="#{msg['view.actions.button.commit']}"
					update="editForm @this" />
			</h:form>
		</p:dialog>


		<p:dialog widgetVar="eventDialog" onHide="myschedule.update();"
			modal="true" header="#{msg['view.actions.calendar.create']}"
			id="eventDia" appendToBody="true" showEffect="clip" hideEffect="clip"
			dynamic="true">
			<p:ajax event="close" listener="#{objectCalendarBean.resetCreatePanel}" />
			<h:form id="commitForm">
				<p:growl id="growl" showDetail="true" autoUpdate="true"
					sticky="true" widgetVar="gowl" />

				<h:inputHidden value="true">
					<f:validator validatorId="reservationValidator" />
					<f:attribute name="since" value="#{since}" />
					<f:attribute name="to" value="#{to}" />
				</h:inputHidden>

				<h:panelGrid id="eventDetails" columns="2">

					<h:outputLabel for="objtext" value="#{msg['properties.object']}:" />
					<h:link id="objtext" outcome="/secured/view/viewobject"
						value="#{objectCalendarBean.event.object.name}">
						<f:param name="id" value="#{objectCalendarBean.event.object.id}" />
					</h:link>

					<h:outputLabel for="usrList" value="#{msg['properties.user']}:" />
					<p:selectOneMenu filter="true" filterMatchMode="startsWith"
						value="#{objectCalendarBean.event.user}" effect="fade"
						var="selusr" converter="userConverter" id="usrlist">
						<f:selectItems value="#{objectCalendarBean.users}" var="usr"
							itemLabel="#{usr.nickname}" itemValue="#{usr}" id="usrList" />
						<p:ajax event="change" update="@this" />
						<p:column>
							<p:button id="viewButton"
								onclick="window.open('view/viewuser.jsf?id=#{selusr.id}'); return false;"
								title="#{msg['view.actions.view']}" icon="ui-icon-search"
								update="@form" ajax="false">
							</p:button>
						</p:column>
						<p:column>
							#{selusr.nickname}
							</p:column>
					</p:selectOneMenu>


					<h:outputLabel for="from"
						value="#{msg['properties.period.since']}:" />
					<p:calendar mindate="#{objectCalendarBean.currentDate}" id="from"
						value="#{objectCalendarBean.event.startDate}" binding="#{since}"
						pattern="#{objectCalendarBean.dateViewPattern}" />

					<h:outputLabel for="to" value="#{msg['properties.period.to']}:" />
					<p:calendar mindate="#{objectCalendarBean.minToDate}" id="to"
						value="#{objectCalendarBean.event.endDate}"  binding="#{to}"
						pattern="#{objectCalendarBean.dateViewPattern}" />

					<h:outputLabel for="objList"
						value="#{msg['properties.reservation.periodical']}:" />
					<p:selectBooleanCheckbox
						value="#{objectCalendarBean.event.periodical}">
						<p:ajax event="change" update="@this" />
						<p:ajax event="change" update="eventDetails" />
					</p:selectBooleanCheckbox>


					<h:outputLabel for="weeks"
						rendered="#{objectCalendarBean.event.periodical}"
						value="#{msg['view.actions.calendar.howmanyweeks']}:" />
					<p:spinner min="1" max="#{objectCalendarBean.maximumWeeks}"
						rendered="#{objectCalendarBean.event.periodical}" id="weeks"
						value="#{objectCalendarBean.event.numberOfWeeks}"
						disabled="#{!objectCalendarBean.event.periodical}" />

					<p:commandButton id="commitButton" icon="ui-icon-check"
						actionListener="#{objectCalendarBean.addEvent}"
						value="#{msg['view.actions.button.commit']}"
						update="commitForm @this" />
				</h:panelGrid>
			</h:form>
		</p:dialog>



	</ui:define>
</ui:composition>
